﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FacialClub.Helpers;
using FacialClub.Models.Entities;
using FacialClub.Models.Interfaces;
using FacialClub.Models.Repositories;
using FacialClub.Models.ViewModels;

namespace FacialClub.Areas.Admin.Controllers
{
    /// <summary>
    /// Access Type Controller
    /// </summary>
    [Authorize]
    public class AccessTypeController : Controller
    {
        #region Fields

        private const int DEFAULTPAGESIZE = 10;
        private readonly IAccessTypeRepository _accessTypeRepository;

        #endregion

        #region Constructors

        /// <summary>
        /// Dependency Injection enabled constructors
        /// </summary>
        public AccessTypeController() : this(new AccessTypeRepository())
        {
        }

        /// <summary>
        /// Access Type Controller Constructor
        /// </summary>
        /// <param name="accessTypeRepository"></param>
        public AccessTypeController(IAccessTypeRepository accessTypeRepository)
        {
            _accessTypeRepository = accessTypeRepository;
        }

        #endregion

        #region Actions

        /// <summary>
        /// Display the listing of Access Type
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string strQuery, int? page)
        {
            var accessType = !string.IsNullOrWhiteSpace(strQuery)
                                                    ? _accessTypeRepository.FindAccessTypeByName(strQuery).OrderBy(
                                                        d => d.CreatedDate)
                                                    : _accessTypeRepository.FindAllAccessType().OrderBy(
                                                        d => d.CreatedDate);
            var paginatedAccessType =
                accessType.ToPagedList(page.HasValue ? page.Value - 1 : 0, DEFAULTPAGESIZE);
            return View(paginatedAccessType);
        }

        /// <summary>
        /// Details of Access Type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int? id)
        {
            if (id != null)
            {
                var accessType = _accessTypeRepository.GetAccessTypeById(id.Value);

                if (accessType == null)
                {
                    return new FileNotFoundResult {Message = "No AccessType found for that id"};
                }

                return View(new AccessTypeViewModel(accessType));
            }

            return new FileNotFoundResult {Message = "No AccessType found due to invalid AccessType id"};
        }

        /// <summary>
        /// Create Access Type
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Create()
        {
            var accessType = new AccessType();
            return View(new AccessTypeViewModel(accessType));
        }

        /// <summary>
        /// Create Access Type
        /// </summary>
        /// <param name="accessType"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Create(AccessType accessType)
        {
            if (ModelState.IsValid)
            {
                accessType.UserCreated = HttpContext.User.Identity.Name;
                accessType.CreatedDate = DateTime.Now.ToSqlDateTime();
                accessType.IsActive = Convert.ToBoolean(Request["IsActive"]);
                _accessTypeRepository.Add(accessType);
                _accessTypeRepository.Save();
                return RedirectToAction("Details", new {id = accessType.AccessTypeId});
            }

            return View(new AccessTypeViewModel(accessType));
        }

        /// <summary>
        /// Edit Access Type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Edit(int id)
        {
            var accessType = _accessTypeRepository.GetAccessTypeById(id);
            return View(new AccessTypeViewModel(accessType));
        }

        /// <summary>
        /// Edit Access Type
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var accessType = _accessTypeRepository.GetAccessTypeById(id);
            if (ModelState.IsValid)
            {
                try
                {
                    accessType.UserModified = HttpContext.User.Identity.Name;
                    accessType.ModifiedDate = DateTime.Now;
                    UpdateModel(accessType, "AccessType");
                    _accessTypeRepository.Save();
                    return RedirectToAction("Details", new {id = accessType.AccessTypeId});
                }
                catch
                {
                    return View(new AccessTypeViewModel(accessType));
                }
            }
            return View(new AccessTypeViewModel(accessType));
        }

        /// <summary>
        /// Delete Access Type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(int id)
        {
            var accessType = _accessTypeRepository.GetAccessTypeById(id);
            return accessType == null ? View("NotFound") : View(new AccessTypeViewModel(accessType));
        }

        /// <summary>
        /// Delete Access Type
        /// </summary>
        /// <param name="id"></param>
        /// <param name="confirmButton"></param>
        /// <returns></returns>
        [HttpPost, Authorize]
        public ActionResult Delete(int id, string confirmButton)
        {
            var accessType = _accessTypeRepository.GetAccessTypeById(id);

            if (accessType != null)
            {
                _accessTypeRepository.Delete(accessType);
                _accessTypeRepository.Save();
                return View("Deleted");
            }
            return View("NotFound");
        }

        /// <summary>
        /// Handle Unknown Action
        /// </summary>
        /// <param name="actionName"></param>
        /// <exception cref="HttpException"></exception>
        protected override void HandleUnknownAction(string actionName)
        {
            throw new HttpException(404, "Action not found");
        }

        #endregion
    }
}